package com.ewhale.feitengguest.ui.user.mine

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.CountDownTimer
import android.text.Editable
import android.text.TextWatcher
import androidx.core.content.ContextCompat
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.presenter.user.mine.MinePresenter
import com.ewhale.feitengguest.view.user.mine.BindAliAccountView
import com.orhanobut.hawk.Hawk
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.CheckUtil
import com.simga.library.utils.HawkKey
import com.simga.library.utils.StringUtil
import kotlinx.android.synthetic.main.activity_bind_ali_account.*

/**
 * 作者：SimGa
 * 日期：2019/12/31
 * 时间：10:47
 * 包名：com.ewhale.feitengguest.ui.user.mine
 */
class BindAliAccountActivity : MBaseActivity<MinePresenter>(), BindAliAccountView, TextWatcher {
    override fun bindSuccess() {

    }

    private var realName = ""
    private var isGetCode = false
    override fun onGetBundle(bundle: Bundle?) {
        super.onGetBundle(bundle)
        realName = bundle!!.getString("realName")
    }

    override fun getViewId(): Int {
        return R.layout.activity_bind_ali_account
    }

    override fun isUseBaseTitleBar(): Boolean {
        return true
    }

    override fun init(savedInstanceState: Bundle?) {
        setTitle("修改支付宝账号")
        et_regist_phone.setText(Hawk.get(HawkKey.USER_PHONE, ""))
    }

    override fun initListener() {
        super.initListener()
        et_regist_phone.addTextChangedListener(this)
        et_code.addTextChangedListener(this)
        btn_getcode.setOnClickListener {
            val registerPhone = et_regist_phone.text.toString()
            if (CheckUtil.isNull(registerPhone)) {
                showToast("请输入手机号码")
                return@setOnClickListener
            }
            if (!StringUtil.isMobile(registerPhone)) {
                showToast("请输入正确手机号码")
                return@setOnClickListener
            }
            presenter.getCode(Hawk.get(HawkKey.USER_AUTHENTICATION, ""), registerPhone)
        }
        btn_next.setOnClickListener {
            if (et_code.text.toString().length < 6) {
                showToast("请输入正确的验证码")
                return@setOnClickListener
            }
            if(!isGetCode){
                showToast("请获取验证码")
                return@setOnClickListener
            }
            BindAliAccountFinishActivity.open(mContext, realName, et_code.text.toString(), et_regist_phone.text.toString())
        }
    }

    override fun getCodeSuccess() {
        isGetCode = true
        showToast("验证码已发送，请注意查收")
        mTimer.start()
    }

    override fun afterTextChanged(s: Editable?) {}
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        if (!checkInput()) {
            btn_next.apply {
                setNormalSolid(Color.parseColor("#EEEEEE"))
                setNormalStroke(1, Color.parseColor("#D4D4D4"))
                setTextColor(Color.parseColor("#BBBBC2"))
                isEnabled = false
            }
        } else {
            btn_next.apply {
                isEnabled = true
                setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color))
                setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color))
                setTextColor(Color.parseColor("#ffffff"))
            }
        }
    }

    private fun checkInput(): Boolean {
        if (CheckUtil.isNull(et_regist_phone.text.toString()) ||
                CheckUtil.isNull(et_code.text.toString()) || et_code.text.toString().length < 6) {
            return false
        }
        return true
    }

    private val mTimer = object : CountDownTimer(60 * 1000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            btn_getcode.apply {
                text = (millisUntilFinished / 1000).toString() + "s"
                setTextColor(ContextCompat.getColor(mContext, R.color.text_666666))
                isEnabled = false
                setNormalSolid(ContextCompat.getColor(mContext, R.color.default_window_color))
                setNormalStroke(1, ContextCompat.getColor(mContext, R.color.default_window_color))
            }
        }

        override fun onFinish() {
            btn_getcode.apply {
                text = "重新获取"
                setTextColor(ContextCompat.getColor(mContext, R.color.main_color))
                isEnabled = true
                setNormalSolid(ContextCompat.getColor(mContext, R.color.white))
                setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color))
            }
        }
    }

    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String, message: String) {
        showErrorMsg(code, message)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        if (requestCode == 1001) {
            finishResult()
        }
    }

    companion object {
        @JvmStatic
        fun open(mContext: MBaseActivity<*>, realName: String, requestCode: Int) {
            val bundle = Bundle()
            bundle.putString("realName", realName)
            mContext.startForResult(bundle, requestCode, BindAliAccountActivity::class.java)
        }
    }
}
